﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace UBCon.Navigation
{
    internal class VerticalPanel:Panel
    {
        #region Constructors
        #endregion

        #region Properties
        #endregion

        #region Depenency Property Changed Callbacks
        #endregion

        #region Overrides

        protected override Size MeasureOverride(Size availableSize)
        {
            UIElement childElement=null;
            double height = 0.0D, width=0;
            
            for (int i = 0; i < InternalChildren.Count; i++)
            {
                childElement = InternalChildren[i];
                childElement.Visibility = System.Windows.Visibility.Visible;
                childElement.Measure(itemSize);
                height += childElement.DesiredSize.Width;
                width = Math.Max(childElement.DesiredSize.Height, width);
            }
            if (!double.IsPositiveInfinity(availableSize.Height)) 
            {
                if (availableSize.Height < height) 
                {
                    double h = 0; int j = 0;
                    while (availableSize.Height + h < height && j < InternalChildren.Count)
                    {
                        childElement = InternalChildren[j];
                        h += childElement.DesiredSize.Width;
                        childElement.Visibility = System.Windows.Visibility.Collapsed;
                        j++;
                    }
                }
                height = Math.Min(height, availableSize.Height);
            }

            desiredSize.Height = height;
            desiredSize.Width = width;
            return desiredSize;
        }

        Rect rect = new Rect();
        protected override Size ArrangeOverride(System.Windows.Size finalSize)
        {
            double y = 0;
            
            for (int i = 0; i < InternalChildren.Count; i++)
            {
                UIElement childElement = null;
                childElement = InternalChildren[i];
                childElement.Measure(itemSize);
                rect.Width = childElement.DesiredSize.Height;
                rect.Height = childElement.DesiredSize.Width;
                rect.X = 0;
                rect.Y = y;
                childElement.Arrange(rect);
                y += childElement.DesiredSize.Width;
            }
            if (!double.IsPositiveInfinity(finalSize.Height))
            {
                y = Math.Max(y, finalSize.Height);
            }
            desiredSize.Height = y;
            return desiredSize;
        }
        #endregion

        #region Private Utilities
        #endregion

        #region Event Handlers
        #endregion

        #region Fields
        private Size itemSize = new Size(double.PositiveInfinity, double.PositiveInfinity);
        private Size desiredSize = new Size();
        #endregion

        #region Events
        #endregion
    }
}
